package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;


    @Override
    public ProductDetailDto getProductDetail(Long id) {
        return null;
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/20 21:32
     * TODO: 展示全部商品，并根据请求信息进行排序，并分页
     */
    @Override
    public AllProductResponse getAllProduct(PageRequest request) {

        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            // 开启分页
            PageHelper.startPage(request.getPage(), request.getSize());

            Example example = new Example(Item.class);
            // 添加排序条件
            if ("1".equals(request.getSort())) {
                example.setOrderByClause("price" + " " + "ASC");
            } else if ("-1".equals(request.getSort())) {
                example.setOrderByClause("price" + " " + "DESC");
            }

            // 添加查询条件
            Example.Criteria criteria = example.createCriteria();

            // 请求参数中有cid时，添加cid条件
            if (request.getCid() != null) {
                criteria.andEqualTo("cid", request.getCid());
            }

            // 根据最低价格
            if (request.getPriceGt() != null) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            // 根据最高价格
            if (request.getPriceLte() != null) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }

            // 查询
            List<Item> items = itemMapper.selectByExample(example);

            // 数据转换
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            PageInfo<Item> itemPageInfo = new PageInfo<>(items);
            long total = itemPageInfo.getTotal();

            // 数据封装
            allProductResponse.setData(productDtos);
            allProductResponse.setTotal(total);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            allProductResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return allProductResponse;
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/21 22:06
     * TODO: 显示热门商品
     */
    @Override
    public Set<PanelDto> getRecommendGoods() {

        Set<PanelDto> panelDtos = new LinkedHashSet<>();

        // 根据热门版块id = 6，查询版块信息
        List<Panel> panels = panelMapper.selectPanelContentById(6);
        System.out.println("panels" + panels);

        for (Panel panel : panels) {
            // 根据热门推荐版块id查询板块下的商品信息
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());

            // 给Panel的属性赋值
            panel.setPanelContentItems(panelContentItems);
            // 数据转换
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            // 数据封装
            panelDtos.add(panelDto);
        }
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
